//
//  ImageAttacher.m
//
//  Created by Alec Flett on 11/23/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ImageAttacher.h"
#import "JSON/JSON.h"


@implementation ImageAttacher

@synthesize delegate = _delegate;
@synthesize didFinishSelector = _didFinishSelector;

- (ImageAttacher*)initWithTopicId:(NSString*)topicId session:(FreebaseSession*)session
{
    if ((self = [super init])) {
        _session = [session retain];
        _topicId = [topicId retain];        
    }
    return self;
}

- (void)imageUploaded:(NSDictionary*)result
{
    NSString* contentId = [result objectForKey:@"id"];
    
    NSDictionary *attach_query = 
    [NSDictionary dictionaryWithObjectsAndKeys:
     _topicId, @"id",
     [NSDictionary dictionaryWithObjectsAndKeys:
      contentId, @"id",
      @"insert", @"connect",
      nil], @"/common/topic/image",
     nil];
    
    NSLog(@"Attaching image to topic");
    [_session mqlwrite:attach_query
                 name:@"attach_image" 
             delegate:_delegate
    didFinishSelector:_didFinishSelector];
    // [self release]; ?
}
    
- (void)errorDidOccur:(id)result request:(id<FreebaseRequest>)request
{
    // just forward to the original error handler
    [_delegate errorDidOccur:result request:request];
}

-(void)dealloc
{
    [super dealloc];
    [_session release];
    [_topicId release];
    [_delegate release];
}

@end
